<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="部门" prop="deptId">
        <el-select v-model="dataForm.deptId">
          <el-option v-for="item in deptOptions" :key="item.id" :value="item.id" :label="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-row>
        <el-col :span="9">
          <el-form-item label="关联部门">
            <el-input style="width: 175px;" v-model="dataForm.linkDeptIds"></el-input>
            <el-input type="textarea" v-model="dataForm.deptName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label-width="0">
            <el-button size="mini" @click="openDeptDialog">选择</el-button>
          </el-form-item>
        </el-col>
      </el-row>



      <el-form-item label="工作类型" prop="jobType">
        <el-select v-model="dataForm.jobType">
          <el-option v-for="item in jobTypeOptions" :key="item.id" :value="item.id" :label="item.jobName"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <!-- 弹窗，查询部门列表 -->
      <search-dept v-if="deptVisible" ref="deptDialog" @refreshData="getDeptData" :company="unit"></search-dept>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj,getJobTypeList} from '@/api/admin/deptlinkdept.js'
    import { getDeptInfoByDeptTypes } from "@/api/admin/dept";
    import SearchDept from '@/components/business/SearchDept'

    export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          deptId: '',
          linkDeptId: '',
          deptName: '',
          jobType: '',
        },
        unit: '',
        deptVisible: false,
        deptOptions: [],
        jobTypeOptions: [],
        dataRule: {
          deptId: [
            { required: true, message: '部门不能为空', trigger: 'blur' }
          ],
          linkDeptIds: [
            { required: true, message: '关联部门不能为空', trigger: 'blur' }
          ],
          jobType: [
            { required: true, message: '工作类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getDeptList();
      this.getJobList();
    },
    components: {
      SearchDept
    },
    methods: {
      init (id) {
        // this.dataForm.id = id || 0
        this.visible = true
        this.dataForm.deptId = ''
        this.dataForm.jobType = ''
        this.dataForm.linkDeptIds = []
        this.dataForm.deptName = []
        // this.$nextTick(() => {
        //   this.$refs['dataForm'].resetFields()
        //   if (this.dataForm.id) {
        //     getObj(this.dataForm.id).then(response => {
        //         this.dataForm = response.data.data
        //     })
        //   }
        // })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putObj(this.dataForm).then(response => {
									if(response.data.code == '0'){
										this.$message.success('修改成功')
										this.visible = false
										this.$emit('refreshDataList')
									}else{
										this.$message.error(response.data.msg)
									}
                })
            } else {
                addObj(this.dataForm).then(response => {
									if(response.data.code == '0'){
										this.$message.success('添加成功')
										this.visible = false
										this.$emit('refreshDataList')
									}else{
										this.$message.error(response.data.msg)
									}
                })
            }
          }
        })
      },
      //获取销售部门列表
      getDeptList() {
        let data = {
          deptTypes: [1],
        }
        getDeptInfoByDeptTypes(data).then(response => {
          if(response.data.code == '0'){
            this.deptOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //获取工作类型列表
      getJobList(){
        getJobTypeList().then(response => {
          if(response.data.code == '0'){
            this.jobTypeOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  打开查询部门弹窗
      openDeptDialog() {
        this.deptVisible = true
        this.$nextTick(() => {
          this.$refs.deptDialog.init()
        })
      },
      //  获取选择部门数据
      getDeptData(data) {
        let ids = []
        let names = []
        if(data.length){
          data.forEach(item => {
            ids.push(item.id)
            names.push(item.name)
          })
        }
        this.dataForm.deptName = names.join(',')
        this.dataForm.linkDeptIds = ids
      },
    }
  }
</script>
